const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
const operateMethod =require('../service/operatef.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        siteOption: [],
        lookStatus: '',
        isArriveScene: 1,
        locationList: [{}],
        locationTypeList: [],
        communicateSituation: [],
        CommunicationSituationList: [],
        remark: '',
        images: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('options', options)
        let that = this
        that.setData({
            taskId: options.taskId,
            operationType: options.type,
            hospitalId: options.hospitalId,
            operationlist: options.operationlist ? JSON.parse(options.operationlist) : [],
            operationTypeCode: options.operationTypeCode,
            activeKey: options.activeKey
        })
        that.getDetailById()
        that.getProjectList('lookAreaStatus')
        that.getProjectList('locationType')
        that.getProjectList('lookAreaCommunicate')
        that.getLookArea()
    },
    //回显
    getLookArea() {
        let that = this
        lbbTaskService.getLookArea({
            taskId: that.data.taskId
        }, function (res) {
            if (res.data) {
                res.data.communicateSituation = res.data.communicateSituation ? res.data.communicateSituation.split(',') : []
                if (res.data.locationList.length == 0 || !res.data.locationList) {
                    res.data.locationList = [{}]
                }
                that.setData({
                    ...res.data
                })
            }
        })
    },
    //获取任务详情
    getDetailById() {
        let that = this
        lbbTaskService.getDetailById({
            taskId: that.data.taskId
        }, function (res) {
            that.setData({
                taskProjectText: res.data.taskProjectText,
                taskClassificationName: res.data.taskClassificationName,
                hospitalName: res.data.hospitalName,
                taskDesc: res.data.taskDesc,
                taskImages: res.data.taskImages ? res.data.taskImages.split(',') : [],
                isUrgent: res.data.isUrgent
            })
        })
    },
    //获取任务详情
    getDetailById() {
        let that = this
        lbbTaskService.getDetailById({
            taskId: that.data.taskId
        }, function (res) {
            that.setData({
                taskProjectText: res.data.taskProjectText,
                taskClassificationName: res.data.taskClassificationName,
                hospitalName: res.data.hospitalName,
                taskDesc: res.data.taskDesc,
                taskImages: res.data.taskImages ? res.data.taskImages.split(',') : [],
                isUrgent: res.data.isUrgent
            })
        })
    },
    //获取数据字典值
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            if (code == 'lookAreaStatus') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    siteOption: res.data
                })
            } else if (code == 'locationType') {
                that.setData({
                    locationTypeList: res.data
                })
            } else if (code == 'lookAreaCommunicate') {
                that.setData({
                    CommunicationSituationList: res.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //看场地情况
    onChangeLookStatus(e) {
        console.log(e)
        this.setData({
            lookStatus: e.detail
        })
    },
    //是否到达现场
    onChangeRadio(e) {
        this.setData({
            isArriveScene: e.detail
        })
    },
    //位置名称
    changeName(e) {
        console.log(e)
        this.data.locationList[e.currentTarget.dataset.index].name = e.detail.value
        this.setData({
            locationList: this.data.locationList
        })
    },
    //位置类型选择
    onChangelocation(e) {
        this.data.locationList[e.currentTarget.dataset.index].locationType = e.detail
        this.setData({
            locationList: this.data.locationList
        })
        console.log(e, this.data.locationList)
    },
    //添加位置
    addPosition() {
        this.data.locationList.push({})
        this.setData({
            locationList: this.data.locationList
        })
    },
    //删除位置
    delPosition(e) {
        this.data.locationList.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            locationList: this.data.locationList
        })
    },
    //沟通情况
    onChangeCheckbox(e) {
        this.setData({
            communicateSituation: e.detail
        })
    },
    //备注
    changeRemark(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    //上传图片
    setImgArr(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1||item.indexOf('wxfile://tmp')!=-1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        wx.showLoading({
            title: '正在上传图片',
            mask: true
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            console.log('item================>', imgIdArray)
            wx.hideLoading()
            oldUpLoadImg.push(...imgIdArray)
            that.setData({
                images: oldUpLoadImg
            })
        });
    },
    //提交
    btnSubmit() {
        let that = this
        let obj = {}
        if (that.data.lookStatus == 'hasLook') {
            let checkList = []
            obj = {
                lookStatus: that.data.lookStatus,
                isArriveScene: that.data.isArriveScene,
                locationList: that.data.locationList,
                communicateSituation: that.data.communicateSituation?.toString(),
                remark: that.data.remark ? that.data.remark : '',
                images: that.data.images ? that.data.images.toString() : '',
                taskId: that.data.taskId
            }
            if (obj.isArriveScene != 0 && obj.isArriveScene != 1) {
                checkList.push(0)
            } else {
                checkList.push(1)
            }
            if (obj.locationList) {
                obj.locationList.forEach(item => {
                    if (!item.name) {
                        checkList.push(0)
                    } else {
                        checkList.push(1)
                    }
                    if (!item.locationType) {
                        checkList.push(0)
                    } else {
                        checkList.push(1)
                    }
                })
            }
            if (!obj.communicateSituation) {
                checkList.push(0)
            } else {
                checkList.push(1)
            }
            if (checkList.indexOf(0) != -1) {
                that.openPrompt()
            } else {
                lbbTaskService.lookArea({
                    areaInfo: JSON.stringify(obj)
                }, function (res) {
                    if (that.data.activeKey == 3) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        operateMethod(that.data.operationlist, that.data.taskId, that.data.hospitalId, that.data.taskType, that.data.operationTypeCode)
                    }
                })
            }
        } else if (that.data.lookStatus == 'notLook') {
            let checkList = []
            obj = {
                lookStatus: that.data.lookStatus,
                isArriveScene: that.data.isArriveScene,
                communicateSituation: that.data.communicateSituation?.toString(),
                remark: that.data.remark ? that.data.remark : '',
                images: that.data.images ? that.data.images.toString() : '',
                taskId: that.data.taskId
            }
            if (obj.isArriveScene != 0 && obj.isArriveScene != 1) {
                checkList.push(0)
            } else {
                checkList.push(1)
            }
            if (!obj.communicateSituation) {
                checkList.push(0)
            } else {
                checkList.push(1)
            }
            if (checkList.indexOf(0) != -1) {
                that.openPrompt()
            } else {
                lbbTaskService.lookArea({
                    areaInfo: JSON.stringify(obj)
                }, function (res) {
                    if (that.data.activeKey == 3) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        operateMethod(that.data.operationlist, that.data.taskId, that.data.hospitalId, that.data.taskType, that.data.operationTypeCode)
                    }
                })
            }
        }else{
            that.openPrompt()
        }
    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})